<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\ding;

use app\common\model\ding\Daili as DailiModel;
use app\store\model\store\User as StoreUserModel;
use think\facade\Db;

/**
 * 商家门店店员模型
 * Class Daili
 * @package app\store\model\store\shop
 */
class Daili extends DailiModel
{
    // 表单验证场景: 新增
    const FORM_SCENE_ADD = 'add';

    // 表单验证场景: 编辑
    const FORM_SCENE_EDIT = 'edit';

    /**
     * 获取列表数据
     * @param array $param
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAll(array $param = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);
        // 查询列表数据
        return $this->with(['user.avatar', 'shop'])

            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc', $this->getPk()])
            ->select();
    }

    /**
     * 获取列表数据
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [])
    {
        // 检索查询条件
        $filter = $this->getFilter($param);
        // 查询列表数据
        return $this->with(['user.avatar'])

            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc', $this->getPk()])
            ->paginate(15);
    }

    /**
     * 检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter($param = [])
    {
        // 默认查询参数
        $params = $this->setQueryDefaultValue($param, [
            'shop_id' => 0,  // 门店id
            'search'  => '', // 搜索关键词: 姓名/手机号
            'status'  => '', // 门店状态(0禁用 1启用)
        ]);
        // 检索查询条件
        $filter = [];
        // 门店id
        $params['shop_id'] > 0 && $filter[] = ['shop_id', '=', (int) $params['shop_id']];
        // 门店id
        $params['shop_id'] > 0 && $filter[] = ['shop_id', '=', (int) $params['shop_id']];
        // 门店状态
        is_numeric($params['status']) && $filter[] = ['status', '=', (int) $params['status']];
        // 搜索关键词
        ! empty($params['search']) && $filter[] = ['real_name|mobile', 'like', "%{$params['search']}%"];
        return $filter;
    }

    /**
     * 新增记录
     * @param $data
     * @return bool
     * @throws \Exception
     */
    public function add($data)
    {
        // 表单验证
        if (! $this->validateForm($data, self::FORM_SCENE_ADD)) {
            return false;
        }
        //代理账号
        $model = new StoreUserModel;

        $data['user_name'] = $data['mobile'];
        //查询user_id
        $model->addDaili($data);

        $data['store_id'] = self::$storeId;
        return $this->save($data);

    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|false|int
     */
    public function edit($data)
    {
      
        if ($data['status'] == 0) {
            Db::name('store_user')->where('user_name', $data['mobile'])->update(['status' => 1]);
        } else {
            Db::name('store_user')->where('user_name', $data['mobile'])->update(['status' => 0]);
        }
        return $this->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        Db::name('store_user')->where('user_name', $this['mobile'])->delete();
        return $this->delete();
    }

    /**
     * 表单验证
     * @param $data
     * @param string $scene
     * @return bool
     */
    private function validateForm($data, $scene = self::FORM_SCENE_ADD)
    {

        if (self::detail(['mobile' => $data['mobile']])) {
            $this->error = '该手机号已存在，无需重复添加';
            return false;
        }
        if ($scene === self::FORM_SCENE_ADD) {
            if (! isset($data['user_id']) || empty($data['user_id'])) {
                $this->error = '请选择用户';
                return false;
            }
            if (self::detail(['user_id' => $data['user_id'], 'is_delete' => 0])) {
                $this->error = '该用户已经是代理，无需重复添加';
                return false;
            }
        }
        return true;
    }
}
